﻿using System;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;

namespace TaxiDispatchClient
{
    public partial class PrivateCarForm : DevExpress.XtraEditors.XtraForm
    {
        public PrivateCarForm()
        {
            InitializeComponent();              
        }

        private Thread _thread;
        private int _pageCount;     //总计多少页
        private int _currentPage;  //当前页码
        private int _pagesize=20;    //每页显示数量
        private int _recordCount; // 数据总条数，即客户总数量
        private bool _isLoad;  // 是否为加载  用于判断是否设置grid样式
        private bool _isSelect; // 是否为查询  用于更新总页数
        private void PhoneUserForm_Load(object sender, EventArgs e)
        {
            _currentPage = 1;
            _isLoad = true;
            _thread = new Thread(BindData);
            _thread.Start();           
        }

        #region  查询当前指定的数据 绑定到Gridview 
        private void BindData()
        {

            try
            {
                 string parameter = radioGroup_parameters.Properties.Items[
                          radioGroup_parameters.SelectedIndex].Value.ToString();
                string parameterValue = textEdit_phoneNumber.Text.Trim();
                DataSet ds = MainForm.PositionService.PrivateCarGetListByPage(
                    _currentPage, _pagesize, parameter, parameterValue);
                if (ds == null || ds.Tables.Count != 1)
                {
                    BeginInvoke(new MethodInvoker(() =>
                                                      {
                                                          gridView1.GroupPanelText = "无数据";
                                                      }));
                    return;
                }
                BeginInvoke(new MethodInvoker(() =>
                {
                    gridControl1.DataSource = ds.Tables[0];                   
                }));
            
                if(_isLoad || _isSelect) //若为页面加载 或者搜索指定用户  则更新页码总数、更新页码下拉框
                {                    

                    _recordCount = MainForm.PositionService.PrivateCarGetCount(
                        parameter, parameterValue);                 
                    _pageCount = int.Parse(Math.Ceiling((double)
                        _recordCount / _pagesize).ToString());
                    BeginInvoke(new MethodInvoker(() =>
                    {
                        gridView1.GroupPanelText = "共计" + _recordCount + "条";
                        label_totalPage.Text = _pageCount.ToString();
                        //label_currentPage.Text = _pageCount > 0 ? "1" : "0";                      
                        
                        comboBox_Pages.Properties.Items.Clear();
                        if(_pageCount>0)
                        {
                            label_currentPage.Text = "1";
                            for (int i = 1; i <=_pageCount; i++)
                            {
                                comboBox_Pages.Properties.Items.Add(i);
                                comboBox_Pages.SelectedIndex = 0;
                            }
                        }
                        else
                        {
                            label_currentPage.Text = "0";
                        }                        
                    }));
                    
                    _isSelect = false;
                    _isLoad = false;

                }
                BeginInvoke(new MethodInvoker(() =>
                                                  {
                                                      btn_prev.Enabled = true;
                                                      btn_next.Enabled = true;
                                                      btn_select.Enabled = true;
                                                      comboBox_Pages.Enabled = true;
                                                  }));
            }
            catch (Exception ex)
            {
                //if(!ex.Message.Contains("正在中止线程"))
                //{
                //    Common.CommonRoutine.HandlingExceptions(ex);                    
                //}
                //else
                //{
                    //Common.CommonRoutine.HandlingExceptions(ex,true);
                    btn_prev.Enabled = true;
                    btn_next.Enabled = true;
                    btn_select.Enabled = true;
                    comboBox_Pages.Enabled = true;
                //}
            }          
           
        }
         #endregion 
 

        private void comboBox_Pages_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(comboBox_Pages.Properties.Items.Count<0) return;
            _currentPage = comboBox_Pages.SelectedIndex + 1;
            btn_prev.Enabled = false;
            btn_next.Enabled = false;
            btn_select.Enabled = false;
            comboBox_Pages.Enabled = false;
            _thread = new Thread(BindData);
            _thread.Start();
            label_currentPage.Text = _currentPage.ToString();
        }

        // 上一页
        private void btn_prev_Click(object sender, EventArgs e)
        {
            if (_currentPage > 1)
            {
                _currentPage--;
                btn_prev.Enabled = false;
                btn_next.Enabled = false;
                btn_select.Enabled = false;
                comboBox_Pages.Enabled = false;
                _thread = new Thread(BindData);
                _thread.Start();
                label_currentPage.Text = _currentPage.ToString();
                comboBox_Pages.SelectedIndex = _currentPage - 1;
            }
        }

        //下一页
        private void btn_next_Click(object sender, EventArgs e)
        {
            if (_currentPage <_pageCount)
            {
                _currentPage++;
                btn_prev.Enabled = false;
                btn_next.Enabled = false;
                btn_select.Enabled = false;
                comboBox_Pages.Enabled = false;
                _thread = new Thread(BindData);
                _thread.Start();
                label_currentPage.Text = _currentPage.ToString();
                comboBox_Pages.SelectedIndex = _currentPage - 1;
            }            
        }

        #region 窗体控件效果
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;

            Cursor = Cursors.Arrow;

        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void PhoneUserForm_MouseClick(object sender, MouseEventArgs e)
        {
            if(e.Button== MouseButtons.Right)
            {
                Close();
            }
        }
        #endregion

        private void pictureEdit2_Click(object sender, EventArgs e)
        {
            PhoneUserReportForm reportForm = new PhoneUserReportForm();
            reportForm.ShowDialog();
        }

        private void btn_exportToExcel_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = "手机用户列表";
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }

        private void PhoneUserForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(_thread!=null)
            {
                _thread.Abort();
            }
        }

        //查询指定手机号码用户
        private void btn_select_Click(object sender, EventArgs e)
        {                      
             _currentPage = 1;
             _isSelect = true;
             btn_prev.Enabled = false;
             btn_next.Enabled = false;
             btn_select.Enabled = false;
             _thread = new Thread(BindData);
             _thread.Start();                       
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow row = gridView1.GetFocusedDataRow();
            if (row==null)
            {
                return;
            }
            //if (e.Button == MouseButtons.Right &&
            //    XtraMessageBox.Show("是否删除该数据?","警告：",MessageBoxButtons.YesNo)
            //    ==DialogResult.Yes)
            //{                 
            //    int id = int.Parse(row["id"].ToString());
            //    if (MainForm.PositionService.PrivateCarDeleteById(id))
            //    {
            //        Thread t = new Thread(BindData);
            //        t.IsBackground = true;
            //        t.Start();
            //        XtraMessageBox.Show("删除成功！");
            //    }
            //    else
            //    {
            //        XtraMessageBox.Show("删除失败！");
            //    }
            //}
        }
       
    }
}